package com.jnu.ilearn.utils;

import android.os.Handler;

import com.jnu.ilearn.bean.Seat;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class SeatUtils {
    int depart=0;
    int use=0;
    public void  DepartSeat(Handler handler, ArrayList<Integer> id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                    Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                    for(int i=0;i<id.size();i++){
                        int ID=id.get(i).intValue();
                        String sql="update seat set Status= 0, LearnWhat= NULL, StartTime= NULL, User=NULL where Id= "+ID;
                        PreparedStatement ptmt=conn.prepareStatement(sql);
                        depart = ptmt.executeUpdate();
                    }
                    DBUtils.disconnect(conn);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public boolean isDepart(){
        if(depart==0){
            return false;
        }
        return true;
    }

    public void  UseSeat(Handler handler, int id, String learnWhat, Calendar time, String user){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                    Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date=time.getTime();
                    String dateText=dateFormat.format(date);
                    String sql="update seat set Status= 1, LearnWhat= ?, StartTime= ?, User=? where Id= ?";
                    PreparedStatement ptmt=conn.prepareStatement(sql);
                    ptmt.setString(1,learnWhat);
                    ptmt.setString(2,dateText);
                    ptmt.setString(3,user);
                    ptmt.setInt(4,id);
                    use = ptmt.executeUpdate();
                    DBUtils.disconnect(conn);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public boolean isUse(){
        if(use==0){
            return false;
        }
        return true;
    }
}
